/*
  设置日期格式化函数
  使用方法:
  Date now = new Date();
  var dateStr = now.format("yyyy-MM-dd hh:mm:ss");
 */
Date.prototype.format = function(format) {
	var o = {
		"M+" : this.getMonth() + 1, // month
		"d+" : this.getDate(), // day
		"h+" : this.getHours(), // hour
		"m+" : this.getMinutes(), // minute
		"s+" : this.getSeconds(), // second
		"q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
		"S" : this.getMilliseconds()
	// millisecond
	};
	if (/(y+)/.test(format))
		format = format.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	for ( var k in o)
		if (new RegExp("(" + k + ")").test(format))
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
					: ("00" + o[k]).substr(("" + o[k]).length));
	return format;
};
//数组删除节点.
Array.prototype.remove = function(dx) {
	if(isNaN(dx)||dx>this.length){return false;}
	for(var i=0,n=0;i<this.length;i++) {
		if(this[i]!=this[dx]) {
			this[n++]=this[i];
		}
	}
	this.length-=1;
};
//数组插入节点.
Array.prototype.insert = function(index, row) {
	if(this.length < index) return false;
	var _tempArray = [];
	for(var i=0;i<index;i++) {
		_tempArray.push(this[i]);
	}
	_tempArray.push(row);
	for(var i=index;i<this.length;i++) {
		_tempArray.push(this[i]);
	}
	for(var i=0;i<_tempArray.length;i++) {
		this[i]=_tempArray[i];
	}
};
/**
 * 判断数据是否在数组中
 */
Array.prototype.inArray = function(value) {
	if(value == null) {
		return false;
	}
	for(var i=0;i<this.length;i++) {
		if(this[i] == value) {
			return true;
		}
	}
	return false;
};
//字符过滤.
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) {  
    if (!RegExp.prototype.isPrototypeOf(reallyDo)) {  
        return this.replace(new RegExp(reallyDo, (ignoreCase ? "gi": "g")), replaceWith);  
    } else {  
        return this.replace(reallyDo, replaceWith);  
    }  
};
//获取关键字数组.
String.prototype.getKeys = function() {  
	var tempStr = this;
	tempStr = tempStr.replace(/\s+/g, "~");
	tempStr = tempStr.replace(/,+/g, "~");
	tempStr = tempStr.replace(/，+/g, "~");
	var strArray = tempStr.split("~");
	var keyArray = [];
	for(var i=0;i<strArray.length;i++) {
		var val = strArray[i]; 
		if(val != "" && !keyArray.inArray(val)) {
			keyArray.push(val);
		}
	}
	return keyArray;
};

$.fn.overflow = function() {
	$(this).each(function(i) {
	    var divH = $(this).height();
	    $(this).wrapInner("<p></p>");
	    var $p = $(this).find("p");
	    while ($p.outerHeight() > divH) {
	    	$p.text($p.text().replace(/(\s)*([a-zA-Z0-9]+|\W)(\.\.\.)?$/, "..."));
	    };
	});
};
